package com.xyht.sca_s.student_manage_system.modules.videophone.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.student.entity.SmsStudentInfo;
import com.xyht.sca_s.student_manage_system.modules.student.entity.SmsUserStudentRelation;
import com.xyht.sca_s.student_manage_system.modules.student.mapper.SmsStudentInfoMapper;
import com.xyht.sca_s.student_manage_system.modules.student.mapper.SmsUserStudentRelationMapper;
import com.xyht.sca_s.student_manage_system.modules.videophone.entity.SmsVideoPhoneLog;
import com.xyht.sca_s.student_manage_system.modules.videophone.entity.req.SmsVideoPhoneLogAddReq;
import com.xyht.sca_s.student_manage_system.modules.videophone.entity.resp.SmsVideoPhoneLogResp;
import com.xyht.sca_s.student_manage_system.modules.videophone.mapper.SmsVideoPhoneLogMapper;
import com.xyht.sca_s.student_manage_system.modules.videophone.service.SmsVideoPhoneLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;


/**
 * <p>
 * 话机记录表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-08-16
 */
@Service
public class SmsVideoPhoneLogServiceImpl extends ServiceImpl<SmsVideoPhoneLogMapper, SmsVideoPhoneLog> implements SmsVideoPhoneLogService {

    @Autowired
    SmsVideoPhoneLogMapper smsVideoPhoneLogMapper;
    @Autowired
    SmsUserStudentRelationMapper smsUserStudentRelationMapper;
    @Autowired
    SmsStudentInfoMapper smsStudentInfoMapper;

    @Override
    public ResponseResult addVideoPhoneLog(SmsVideoPhoneLogAddReq addReq) {
        if (isNullOrEmpty(addReq.getPin()) || isNullOrEmpty(addReq.getBillCaller()) || isNullOrEmpty(addReq.getBillCalled())
                || isNullOrEmpty(addReq.getBillCallType()) || isNullOrEmpty(addReq.getStart())
                || isNullOrEmpty(addReq.getEnd()) || isNullOrEmpty(addReq.getDuration())
                || isNullOrEmpty(addReq.getPhoneBill()) || isNullOrEmpty(addReq.getRealName())
                || isNullOrEmpty(addReq.getDeviceSn()) || isNullOrEmpty(addReq.getDeductionStatus())
                || isNullOrEmpty(addReq.getOrganizationId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsVideoPhoneLog smsVideoPhoneLog = new SmsVideoPhoneLog();
        BeanUtils.copyProperties(addReq, smsVideoPhoneLog);
        smsVideoPhoneLogMapper.insert(smsVideoPhoneLog);
        return CommonResult.success();
    }

    @Override
    public ResponseResult selectInfo(String student_name, String student_pin, String org_id, Integer pageSize, Integer pageNum) {
        QueryWrapper<SmsVideoPhoneLog> queryWrapper = new QueryWrapper<>();
        if (!isNullOrEmpty(student_name)) {
            queryWrapper.lambda().like(SmsVideoPhoneLog::getRealName, student_name);
        }
        if (!isNullOrEmpty(student_pin)) {
            queryWrapper.lambda().like(SmsVideoPhoneLog::getPin, student_pin);
        }
        if (!isNullOrEmpty(org_id)) {
            queryWrapper.lambda().like(SmsVideoPhoneLog::getOrganizationId, org_id);
        }
        queryWrapper.orderByDesc("create_time");
        Page<SmsVideoPhoneLog> page = new Page<>(pageNum,pageSize);
        List<SmsVideoPhoneLogResp> collect;

        smsVideoPhoneLogMapper.selectPage(page, queryWrapper);
        List<SmsVideoPhoneLog> records = page.getRecords();
        collect = records.stream().map(appApplication -> {
            SmsVideoPhoneLogResp appApplicationResp = new SmsVideoPhoneLogResp();
            BeanUtils.copyProperties(appApplication, appApplicationResp);
            return appApplicationResp;
        }).collect(Collectors.toList());

        int total = (int) page.getTotal();
        return CommonResult.success(collect, total);
    }

    @Override
    public ResponseResult selectOwn(String user_id, Integer pageSize, Integer pageNum) {
        List<SmsVideoPhoneLogResp> collect = new ArrayList<>();
        QueryWrapper<SmsUserStudentRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SmsUserStudentRelation::getUserId,user_id);
        SmsUserStudentRelation relation = smsUserStudentRelationMapper.selectOne(queryWrapper);
        if(isNullOrEmpty(relation)){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        QueryWrapper<SmsStudentInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(SmsStudentInfo::getId,relation.getStuId());
        SmsStudentInfo info = smsStudentInfoMapper.selectOne(queryWrapper1);
        if(isNullOrEmpty(info)){
            return CommonResult.failed(CommonCodeEnum.DATA_NOT_FOUND);
        }
        QueryWrapper<SmsVideoPhoneLog> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().eq(SmsVideoPhoneLog::getPin,info.getStuNo());
        //List<SmsVideoPhoneLog> list = smsVideoPhoneLogMapper.selectList(queryWrapper2);
//        if(isNullOrEmpty(list)&&list.size()<1){
//            return CommonResult.failed(CommonCodeEnum.VIDEO_PHONE_NOT_EXIST);
//        }
        Page<SmsVideoPhoneLog> page = new Page<>(pageNum,pageSize);
        smsVideoPhoneLogMapper.selectPage(page,queryWrapper2);
        collect = page.getRecords().stream()
                .map(smsVideoPhoneLog -> {
                    SmsVideoPhoneLogResp smsVideoPhoneLogResp = new SmsVideoPhoneLogResp();
                    BeanUtils.copyProperties(smsVideoPhoneLog, smsVideoPhoneLogResp);
                    return smsVideoPhoneLogResp;
                }).collect(Collectors.toList());
        int total = (int) page.getTotal();
        return CommonResult.success(collect, total);
    }
}
